package com.ygo.feihua;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import cn.bmob.v3.datatype.BmobFile;
import cn.bmob.v3.exception.BmobException;
import cn.bmob.v3.listener.SaveListener;
import cn.bmob.v3.listener.UploadFileListener;

import java.io.File;
import com.ygo.feihua.BmobTable.GuangGao;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.Adapter;

public class GuanggaoAdd extends AppCompatActivity implements OnClickListener,OnItemSelectedListener
{
	private Button ggadd_add;
	private EditText ggadd_lj,ggadd_name;
	private Spinner ggadd_yc,ggadd_lx,ggadd_nrlx;
	private ImageView ggadd_tp;
	private String tppath;
	private Gongju gj;
	int yc,lx,nrlx;

	private String name;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		// TODO: Implement this method
		super.onCreate(savedInstanceState);
		setContentView(R.layout.guanggao_add);
		csh();
		
		gj.cshToolbar((Toolbar) findViewById(R.id.toolbar),"返回");	
		
	}
	private void csh(){
		ggadd_add=(Button) findViewById(R.id.ggadd_add);
		ggadd_lj=(EditText) findViewById(R.id.ggadd_lj);
		ggadd_yc=(Spinner) findViewById(R.id.ggadd_yc);
		ggadd_tp=(ImageView) findViewById(R.id.ggadd_tp);
		ggadd_lx=(Spinner) findViewById(R.id.ggadd_lx);
		ggadd_nrlx=(Spinner) findViewById(R.id.ggadd_nrlx);
		ggadd_name=(EditText) findViewById(R.id.ggadd_name);
		
		gj=Gongju.getdx(this);
		
		ggadd_add.setOnClickListener(this);
		ggadd_tp.setOnClickListener(this);
		ggadd_yc.setOnItemSelectedListener(this);
		ggadd_lx.setOnItemSelectedListener(this);
		ggadd_nrlx.setOnItemSelectedListener(this);
	}

	@Override
	public void onItemSelected(AdapterView<?> p1, View p2, int p3, long p4)
	{
		switch(p1.getId()){
			case R.id.ggadd_yc:
				yc=p3;
				break;
			case R.id.ggadd_lx:
				lx=p3;
				break;
			case R.id.ggadd_nrlx:
				nrlx=p3;
				break;
		}
		// TODO: Implement this method
	}

	@Override
	public void onNothingSelected(AdapterView<?> p1)
	{
		// TODO: Implement this method
	}
	
	@Override
	public void onClick(View p1)
	{
		switch(p1.getId()){
			case R.id.ggadd_add:
			
				final String lj;
				
					lj=ggadd_lj.getText().toString().trim();
				 name=ggadd_name.getText().toString().trim();
				if (tppath != null && !lj.equals("")&&!name.equals(""))
				{
					final BmobFile bf=new BmobFile(new File(tppath));
					bf.uploadblock(new UploadFileListener(){

							@Override
							public void done(BmobException p1)
							{
								if(p1==null)
								{				
									GuangGao gg=new GuangGao();
									gg.setGg_tp(bf);
									gg.setGg_wz(lj);
									gg.setGg_yc(yc);
									gg.setGg_lx(lx);
									gg.setGg_nrlx(nrlx);
									gg.setGg_name(name);
									gg.save(new SaveListener<String>(){

											@Override
											public void done(String p1, BmobException p2)
											{
												if(p2==null){
													gj.show("添加成功");
													finish();
												}else{
													gj.show("添加失败,原因为"+p2);
												}
												// TODO: Implement this method
											}
										});
									
								}else{
									gj.show("上传图片失败,原因为"+p1);
								}
								// TODO: Implement this method
							}
						});
				}
					else{
						gj.show("信息未填写完整");
					}
				break;
			case R.id.ggadd_tp:
				
				Intent i=new Intent(Intent.ACTION_PICK, null);
				i.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
				
				startActivityForResult(i, 234);
				break;
		}
		// TODO: Implement this method
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		// TODO: Implement this method
		super.onActivityResult(requestCode, resultCode, data);
		if(requestCode==234){
			if(data!=null){
				Uri path=data.getData();
				String []imgs={MediaStore.Images.Media.DATA};//将图片URI转换成存储路径  
				Cursor cursor=this.managedQuery(path, imgs, null, null, null);  
				int index=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);  
				cursor.moveToFirst();  
				tppath=cursor.getString(index);  
				BitmapFactory.Options options=new BitmapFactory.Options();
				options.inSampleSize = 2;
				Bitmap bm=BitmapFactory.decodeFile(tppath, options);
				ggadd_tp.setImageBitmap(bm);
			}
		}
	}

	
}
